
#include <iostream>
#include <memory>

// Step 1: 定义产品接口
class Drink {
public:
    virtual ~Drink() = default;
    virtual void prepare() const = 0;  // 每种饮料的具体准备方法
};

// Step 2: 创建具体产品类
class Coffee : public Drink {
public:
    void prepare() const override {
        std::cout << "Preparing Coffee: grinding beans, brewing coffee.\n";
    }
};

class Tea : public Drink {
public:
    void prepare() const override {
        std::cout << "Preparing Tea: boiling water, steeping tea leaves.\n";
    }
};

// Step 3: 创建工厂接口
class DrinkFactory {
public:
    virtual ~DrinkFactory() = default;
    virtual std::unique_ptr<Drink> createDrink() const = 0;  // 工厂方法
};

// Step 4: 创建具体工厂类
class CoffeeFactory : public DrinkFactory {
public:
    std::unique_ptr<Drink> createDrink() const override {
        return std::make_unique<Coffee>();  // 返回具体的产品对象
    }
};

class TeaFactory : public DrinkFactory {
public:
    std::unique_ptr<Drink> createDrink() const override {
        return std::make_unique<Tea>();  // 返回具体的产品对象
    }
};

// Step 5: 客户端代码
void orderDrink(const DrinkFactory& factory) {
    std::unique_ptr<Drink> drink = factory.createDrink();
    drink->prepare();
}

int main() {
    CoffeeFactory coffeeFactory;
    TeaFactory teaFactory;

    std::cout << "Ordering a coffee:\n";
    orderDrink(coffeeFactory);  // 通过工厂创建Coffee对象

    std::cout << "\nOrdering a tea:\n";
    orderDrink(teaFactory);  // 通过工厂创建Tea对象

    return 0;
}